//
//  DHQrCoceViewController.m
//  NavigationDemo
//
//  Created by 商广硕 on 2022/7/16.
//

#import "DHQrCoceViewController.h"

@interface DHQrCoceViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *qrCodeImg;
@property (weak, nonatomic) IBOutlet UIImageView *barCode;

@end

@implementation DHQrCoceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.barTitle=@"二维码";
    
    NSArray *filter = [CIFilter filterNamesInCategory:kCICategoryBuiltIn];
//    NSLog(@"%@", filter);
    // 二维码过滤器
    CIFilter *filterImage = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    // 将二位码过滤器设置为默认属性
    [filterImage setDefaults];
    // 将文字转化为二进制
    NSData *dataImage = [@"二维码内容" dataUsingEncoding:NSUTF8StringEncoding];
    // 打印输入的属性
    NSLog(@"%@", filterImage.inputKeys);
    // KVC 赋值
    [filterImage setValue:dataImage forKey:@"inputMessage"];
    // 取出输出图片
    CIImage *outputImage = [filterImage outputImage];
    outputImage = [outputImage imageByApplyingTransform:CGAffineTransformMakeScale(20, 20)];
    // 转化图片
    UIImage *image = [UIImage imageWithCIImage:outputImage];

    // 为二维码加自定义图片

    // 开启绘图, 获取图片 上下文<图片大小>
    UIGraphicsBeginImageContext(image.size);
    // 将二维码图片画上去
    [image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
    // 将小图片画上去
    UIImage *smallImage = [UIImage imageNamed:@"result"];
    [smallImage drawInRect:CGRectMake((image.size.width - 100) / 2, (image.size.width - 100) / 2, 100, 100)];
    // 获取最终的图片
    UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
    // 关闭上下文
    UIGraphicsEndImageContext();
    // 显示
    self.qrCodeImg.image = finalImage;
    
    
//条形码
    
    NSData *data = [@"777777777777777777" dataUsingEncoding:NSISOLatin1StringEncoding allowLossyConversion:false];

    CIFilter *filter1 = [CIFilter filterWithName:@"CICode128BarcodeGenerator"];
      [filter1 setValue:data forKey:@"inputMessage"]; // 设置条形码内容
//          [filter1 setValue:@(50) forKey:@"inputQuietSpace"]; // 设置条形码上下左右margin值
          [filter1 setValue:@(88) forKey:@"inputBarcodeHeight"]; // 设置条形码高度
      CIImage *ciImage = filter1.outputImage;
//      CGFloat scaleX = width/ciImage.extent.size.width;
//      CGFloat scaleY = height/ciImage.extent.size.height;

    
    CGFloat scaleX = self.barCode.bounds.size.width/ciImage.extent.size.width;
        CGFloat scaleY = self.barCode.bounds.size.height/ciImage.extent.size.height;
    ciImage = [ciImage imageByApplyingTransform:CGAffineTransformScale(CGAffineTransformIdentity, scaleX, scaleY)];

        UIImage *returnImage = [UIImage imageWithCIImage:ciImage];
  
    // 显示
    self.barCode.image = returnImage;
}



@end
